Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Código abreviado de WooCommerce para ajaxificar el mensaje que muestra: "Necesita gastar para obtener envío gratis"

Modifiqué este script para crear un código abreviado que muestre cuánto debe gastar el cliente para obtener el envío gratis.

 /** * @author Rodolfo Melogli * @compatible WooCommerce 3.9 * @donate $9 https://businessbloomer.com/bloomer-armada/ */ add_shortcode ('woo_freeshipping_target', 'woo_freeship_target' ); function woo_freeship_target() { ob_start(); $min_amount = 279; //change this to your free shipping threshold $current = WC()->cart->subtotal; if ( $current < $min_amount ) { $added_text = wc_price( $min_amount - $current ); $notice = sprintf( $added_text ); echo '<span> You need to add '.$notice.'</span>'; } else if ( $current >= $min_amount ) { echo '<span>Free Shipping!</span>'; } return ob_get_clean(); }

Pero no puedo entender cómo puedo hacer que se actualice automáticamente cuando se modifica la cantidad de artículos. ¿Alguna idea?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Para ajaxificar su mensaje para que se actualice cuando cambie el subtotal (a través de ajax) use el gancho de filtro woocommerce_add_to_cart_fragments

Entonces obtienes:

 function custom_message() { // Initialize $message = __( 'Default message', 'woocommerce' ); // True if ( WC()->cart ) { // Change this to your free shipping threshold $min_amount = 279; // Get cart subtotal $subtotal = WC()->cart->get_subtotal(); if ( $subtotal < $min_amount ) { $message = sprintf( __( 'You need to add %s to get free shipping', 'woocommerce' ), wc_price( $min_amount - $subtotal ) ); } else { $message = __( 'Free shipping', 'woocommerce' ); } } return $message; } // Refreshing on cart ajax events function filter_woocommerce_add_to_cart_fragments( $fragments ) { $fragments['div.display-message'] = '<div class="display-message">' . custom_message() . '</div>'; return $fragments; } add_filter( 'woocommerce_add_to_cart_fragments', 'filter_woocommerce_add_to_cart_fragments', 10, 1 ); // Shortcode function display_my_message() { return '<div class="display-message">' . custom_message() . '</div>'; } // Register shortcode add_shortcode( 'display_message', 'display_my_message' );

USO DE CÓDIGO CORTO

En una página existente:

[display_message]

O en PHP:

echo do_shortcode("[display_message]");


Relacionado: código abreviado de WooCommerce para ajaxificar el mensaje que muestra: "Compre X más productos para obtener un descuento"

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!